﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Toto.Componentes
{
    class Bola
    {
        private float raio;
        public float speed { get; set; }
        private float accel;
        Vector2 Direction;
        Texture2D imagem;
        Vector2 position;
        Vector2 velocidade = new Vector2 (5,5);
        Rectangle campo, retangulo;
        //SoundEffect efeito;

        public Bola(Rectangle campo, Rectangle retangulo)
        {
            this.imagem = Textura.texturaBola;
            this.raio = imagem.Width / 2;
            accel = -15.0f;
            speed = 250.0f;
            this.campo = campo;
            this.retangulo = retangulo;
            this.Direction = new Vector2(-1.0f, 0.0f);
            position = new Vector2(campo.Width / 2 + imagem.Width/6, campo.Height / 2 - imagem.Height / 2);

        }

        public void Update(GameTime gameTime)
        {

            float time = (float)(gameTime.ElapsedGameTime.TotalMilliseconds / 2000.0f);
            // Atualiza a posição do carro
            // S = So + vo * t + (a * t * t) / 2

            while (speed > 400)
            {
                speed = speed-1;

            }
            if (speed > 0)
            {
                position.X += Direction.X * (speed * time) + ((accel * (float)Math.Pow(time, 2)) * 0.5f);
                position.Y += Direction.Y * (speed * time) + ((accel * (float)Math.Pow(time, 2)) * 0.5f);
                // Atualiza velocidade do objeto
                // v = vo + a * t
                speed += accel * time;
            }
            else
            {
                speed = 0.0f;
            }


            //Console.WriteLine(retangulo.X + " " + campo.X);

            //atualiza a posicao do retangulo da bola de acordo com  a sua posicao 
           /* this.size.X = (int)position.X;
            this.size.Y = (int)position.Y;*/

            
            #region Manter na Janela
            
            if (position.Y <= campo.Y)
            {
                Direction = new Vector2(Direction.X , Direction.Y * (-1));

            }
            if (position.Y >= (campo.Y + campo.Height - retangulo.Height) )
            {
                Direction = new Vector2(Direction.X , Direction.Y * (-1));

            }

            if (position.X <= campo.X)
            {
                Direction = new Vector2(Direction.X * (-1), Direction.Y - 1);

            }

            if (position.X >= (campo.X + campo.Width - retangulo.Width))
            {
                Direction = new Vector2(Direction.X * (-1), Direction.Y - 1); 

            }
             
            #endregion


        }

        public void Draw(SpriteBatch spriteBatch, GameTime gameTime)
        {
            spriteBatch.Draw(imagem, position, retangulo, Color.White);
            //spriteBatch.Draw(imagem, position , Color.White);
        }

        #region [ Bounding ]
        // Retorna uma esfera que delimita a área do objeto
        public BoundingSphere Areabola()
        {
            return new BoundingSphere(new Vector3(position.X + imagem.Width / 2, position.Y + imagem.Height / 2, 0.0f),
                this.raio);
        }
        #endregion

        #region [ Bounding ]
        // Retorna uma esfera que delimita a área do objeto
        public BoundingSphere Areacampo()
        {
            return new BoundingSphere(new Vector3(campo.X + Textura.texturaCampo.Width / 2, campo.Y + Textura.texturaCampo.Height / 2, 0.0f),
                Textura.texturaCampo.Width / 2);
        }
        #endregion

        public void chutaBola(Boneco bn)
        { 
        if (bn.Area().Intersects(this.Areabola()))
        {

                        Vector2 ballCenter = new Vector2(Areabola().Center.X, Areabola().Center.Y);
                        Vector2 bonecoCenter = new Vector2(bn.Posicao.X + (bn.bTextura.Width/3) / 2,
                            bn.Posicao.Y + bn.bTextura.Height / 2);

                        float dist = (float)Math.Atan2(ballCenter.Y - bonecoCenter.Y, ballCenter.X - bonecoCenter.X);
                        Direction = new Vector2((float)Math.Cos(dist), (float)Math.Sin(dist));
                        speed = speed + 50;
        }
        
        
        }
    }


}
